﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Device
{
    public partial class RetrievePassword : System.Web.UI.Page
    {
        string CodeError = "校验码错误！请重新输入！";
        string GetCode = "请获取验证码！";
        protected void Page_Load(object sender, EventArgs e)
        {
            Image1.ImageUrl = "../WebForm1.aspx";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["username"] = TextBox1.Text.Trim();
            Session["email"] = TextBox2.Text.Trim();
            var checkCode = TextBox3.Text.Trim();
            RegionCode.Result result = RegionCode.CheckCodeSame(checkCode);
            switch (result)
            {
                case RegionCode.Result.Fail:
                    Util.AlertString(CodeError, Response);
                    break;
                case RegionCode.Result.Success:
                    CheckUser();
                    break; 
                case RegionCode.Result.CodeNull:
                    Util.AlertString(GetCode, Response);
                    break;
            }
        }

        //返回上一页
        protected void Button3_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Session["time"] = DateTime.Now.AddMinutes(5).ToString();
            var checkCode = TextBox4.Text.Trim();
            RegionCode.Result result = RegionCode.CheckCodeSame(checkCode);
            switch (result)
            {
                case RegionCode.Result.Fail:
                    Util.AlertString(CodeError, Response);
                    break;
                case RegionCode.Result.Success:
                    CheckQueAn();
                    break;
                case RegionCode.Result.CodeNull:
                    Util.AlertString(GetCode, Response);
                    break;
            }
        }

        //检查用户是否存在
        void CheckUser()
        {
            string Str = "抱歉!您的昵称和注册时的邮箱不匹配";            
            using (Model1Container db = new Model1Container())
            {
                EquipmentMaster sh = new EquipmentMaster();
                var username = TextBox1.Text.Trim();
                var email = TextBox2.Text.Trim();
                List<EquipmentMaster> sheb = (from u in db.EquipmentMaster集 where u.email == email && u .username == username  select u).ToList();
                if (sheb.Count() > 0)
                {
                    sh = db.EquipmentMaster集.FirstOrDefault(cc => cc.email == email);
                    Label1.Text = sh.question;
                    Image2.ImageUrl = "../WebForm1.aspx";
                    MultiView1.ActiveViewIndex = 1;
                }
                else
                {
                    Util.AlertString(Str, Response);
                }
            }
        }
        //查询用户id

        
        //查询问题与答案是否相符合
        void CheckQueAn()
        {
            string Str = "抱歉!回答错误!";
            EquipmentMaster sh = new EquipmentMaster();
            var name = TextBox1.Text.Trim();
            var email = TextBox2.Text.Trim();
            var question = Label1.Text.Trim();
            var answer = TextBox5.Text.Trim();
            Session["userid"] = DeviceMaster.CheckUserId(email);
            var userid = DeviceMaster.CheckUserId(email);
            var time = (string)Session["time"];
            DeviceMaster.AuthResult result = DeviceMaster.queryUserQueAnser(email, question, answer);
            switch (result)
            {
                case DeviceMaster.AuthResult.Success:
                    Util.sendEmail(name, email, time, userid);
                    MultiView1.ActiveViewIndex = 2;
                    break;
                case DeviceMaster.AuthResult.DoesnotMatch:
                    Util.AlertString(Str, Response);
                    break;
            }
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            Image1.ImageUrl = "../WebForm1.aspx";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Image1.ImageUrl = "../WebForm1.aspx";
        }
 
    }
}